package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.dto.SetmealDto;
import com.itheima.common.entity.PageResult;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.mapper.CheckGroupAndSetmealMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 套餐Service业务层处理
 *
 * @author itheima
 * @date 2025-03-23
 */
@Service
public class SetmealServiceImpl implements ISetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckGroupAndSetmealMapper checkGroupAndSetmealMapper;


    @Override
    public Setmeal selectDetilesById(Integer id) {
        return checkGroupAndSetmealMapper.selectCheckGroupAndCheckItemBySetmealId(id);
    }

    /**
     * 查询套餐
     *
     * @param id 套餐主键
     * @return 套餐
     */
    @Override
    public Setmeal selectSetmealById(Integer id) {
        return setmealMapper.selectSetmealById(id);
    }

    @Override
    public List<Setmeal> selectList() {
        return setmealMapper.selectSetmealList(null);
    }

    /**
     * 查询套餐列表
     *
     * @param setmealDto 套餐
     * @return 套餐
     */
    @Override
    public PageResult selectSetmealList(SetmealDto setmealDto) {
        if (setmealDto == null) {
            setmealDto = new SetmealDto();
        }

        if (setmealDto.getCurrentPage() == null || setmealDto.getPageSize() == null) {
            setmealDto.setCurrentPage(1);
            setmealDto.setPageSize(10);
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);

        PageHelper.startPage(setmealDto.getCurrentPage(), setmealDto.getPageSize());

        Page<Setmeal> setmeals = (Page<Setmeal>) setmealMapper.selectSetmealList(setmeal);

        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }

    /**
     * 新增套餐
     *
     * @param checkgroupIds 检查组id集合
     * @param setmeal       套餐
     * @return 结果
     */
    @Override
    public int insertSetmeal(List<Integer> checkgroupIds, Setmeal setmeal) {

        int i = setmealMapper.insertSetmeal(setmeal);

        if (checkgroupIds != null && checkgroupIds.size() > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Integer setmealId = setmeal.getId();
                checkGroupAndSetmealMapper.insert(checkgroupId, setmealId);
            }
        }

        if (i > 0) {
            //将成功添加到数据库中的图片路径url，使用redisTemplate保存到redis中
            redisTemplate.opsForSet().add("save", setmeal.getImg());
        }

        return i;
    }

    /**
     * 修改套餐
     * @param checkgroupIds 检查组id集合
     * @param setmeal 套餐
     * @return 结果
     */
    @Override
    public int updateSetmeal(List<Integer> checkgroupIds, Setmeal setmeal) {

        Integer setmealId = setmeal.getId();
        checkGroupAndSetmealMapper.delete(setmealId);

        if (checkgroupIds != null && checkgroupIds.size() > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                checkGroupAndSetmealMapper.insert(checkgroupId, setmealId);
            }
        }

        return setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 批量删除套餐
     *
     * @param ids 需要删除的套餐主键
     * @return 结果
     */
    @Override
    public int deleteSetmealByIds(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            throw new RuntimeException("请选择要删除的套餐");
        }
        for (Integer id : ids) {
            List<Integer> checkGroupIds = checkGroupAndSetmealMapper.selectCheckGroupIdBySetmealId(id);
            if (checkGroupIds != null) {
                throw new RuntimeException("该套餐已被关联，不能删除");
            } else {
                setmealMapper.deleteSetmealById(id);
            }
        }
        return 1;
    }

    /**
     * 删除套餐信息
     *
     * @param id 套餐主键
     * @return 结果
     */
    @Override
    public int deleteSetmealById(Integer id) {
        return setmealMapper.deleteSetmealById(id);
    }


    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    //图片上传
    public String upload(MultipartFile imgFile) {
        try {
            String url = aliOssUtil.upload(imgFile.getBytes(), UUID.randomUUID().toString().replace("-", ""));

            //使用redisTemplate将url保存到redis中
            redisTemplate.opsForSet().add("upload", url);

            return url;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
